﻿$(document).ready(function () {
    GetCategories();
    getCategoriesforDDL("DDL_Category");
    GetSubCategories();
});
function saveCategory() {
    var category = new Object();
    category.CategoryName = $("[id$='txtCategoryName']").val();
    $.ajax({
        url: "/Items/AddEditCategory.aspx/SaveItemCategory",
        type: "POST",
        dataType: "json",
        data: JSON.stringify({ category: category }),
        contentType: "application/json; charset=utf-8"
    }).done(function (data) {
        showSuccessMsg("Category Added Successfully!!!!!");
        $("#divCategoryWrapper").find("input[type=text]").val("");
        GetCategories();
    });
}


function GetCategories() {
    $.ajax({
        url: "/Items/AddEditCategory.aspx/SearchCategories",
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8"
    }).done(function (data) {
        var Categories = data.d;
        for (var b = 0; b < Categories.length; b++) {
            var html = "<tr>";
            html += "<td>" + Categories[b].CategoryId + "</td>";
            html += "<td>" + Categories[b].CategoryName + "</td>";
            html += "<td><span onclick=DeleteCategory(this," + Categories[b].CategoryId + ")>Delete<span></td>";
            html += "</tr>";
            $("#tbodyCategories").append(html);
        }
    });
}

function DeleteCategory(elem,id) {
    $.ajax({
        url: "/Items/AddEditCategory.aspx/DeleteCategory",
        type: "POST",
        dataType: "json",
        data: JSON.stringify({ id: id }),
        contentType: "application/json; charset=utf-8"
    }).done(function (data) {
        showSuccessMsg("Category Deleted Successfully!!!!!");
        $(elem).closest("tr").remove();
    });
}

function saveSubCategory() {
    var subcategory = new Object();
    subcategory.SubCategoryName = $("[id$='txtSubCategory']").val();
    subcategory.CategoryId = $("[id$='DDL_Category']").val();
    $.ajax({
        url: "/Items/AddSubCategory.aspx/SaveItemSubCategory",
        type: "POST",
        dataType: "json",
        data: JSON.stringify({ subcategory: subcategory }),
        contentType: "application/json; charset=utf-8"
    }).done(function (data) {
        showSuccessMsg("Sub Category Added Successfully!!!!!");
        $("#divSubCategoryWrapper").find("input[type=text]").val("");
        GetSubCategories();
    });
}

function GetSubCategories() {
    var category_id = $("[id$='DDL_Category']").val();
    if (category_id==null) {
        category_id = 0;
    }
    $.ajax({
        url: "/Items/AddSubCategory.aspx/SearchSubCategories",
        type: "POST",
        dataType: "json",
        data: JSON.stringify({ category_id: category_id }),
        contentType: "application/json; charset=utf-8"
    }).done(function (data) {
        $("#tbodySubCategories").html("");
        var SubCategories = data.d;
        for (var b = 0; b < SubCategories.length; b++) {
            var html = "<tr>";
            html += "<td>" + SubCategories[b].SubCategoryId + "</td>";
            html += "<td>" + SubCategories[b].SubCategoryName + "</td>";
            html += "<td>" + SubCategories[b].CategoryName + "</td>";
            html += "<td><span onclick=DeleteCategory(this," + SubCategories[b].SubCategoryId + ")>Delete<span></td>";
            html += "</tr>";
            $("#tbodySubCategories").append(html);
        }
    });
}
function DeleteSubCategory(elem, id) {
    $.ajax({
        url: "/Items/AddSubCategory.aspx/DeleteSubCategory",
        type: "POST",
        dataType: "json",
        data: JSON.stringify({ id: id }),
        contentType: "application/json; charset=utf-8"
    }).done(function (data) {
        showSuccessMsg("Sub Category Deleted Successfully!!!!!");
        $(elem).closest("tr").remove();
    });
}


function saveManufacturer() {
    var category = new Object();
    category.CategoryName = $("[id$='txtManufacturerName']").val();
    $.ajax({
        url: "/Items/AddEditManufacturer.aspx/SaveManufacturer",
        type: "POST",
        dataType: "json",
        data: JSON.stringify({ category: category }),
        contentType: "application/json; charset=utf-8"
    }).done(function (data) {
        showSuccessMsg("Manufacturer Added Successfully!!!!!");
        $("#divManufacturerWrapper").find("input[type=text]").val("");
        //GetManufacturer();
    });
}
